/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.drawing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JCheckBox;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import twaver.DataBoxAdapter;
import twaver.DataBoxEvent;
import twaver.EditableFilter;
import twaver.Element;
import twaver.Layer;
import twaver.PopupMenuGenerator;
import twaver.ResizableFilter;
import twaver.SelectionChangedInterceptor;
import twaver.TDataBox;
import twaver.TView;
import twaver.Text;
import twaver.network.InteractionEvent;
import twaver.network.InteractionListener;
import twaver.network.LayerManagerPane;
import twaver.network.TNetwork;
import twaver.network.action.ClearDataBoxAction;
import twaver.network.background.ColorBackground;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import twaver.tree.TTree;
import demo.DemoPane;
import demo.DemoUtil;

public class DrawingEditorDemo extends DemoPane{

    private TDataBox box = new TDataBox("Drawing Editor");
    private TTree tree = new TTree(box);
    private TPropertySheet sheet = new TPropertySheet(box);
    private TNetwork network = new TNetwork(box);
    private JTextArea intercationArea = DemoUtil.createColorTextArea();
    private JTextArea selectionArea = DemoUtil.createColorTextArea();
    private JCheckBox resizable = new JCheckBox("Resizable", true);
    private JCheckBox showGrid = new JCheckBox("ShowGrid");
    private GridSnapper snapper = new GridSnapper(network);
    
	public DrawingEditorDemo(){
		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
				new JScrollPane(intercationArea), new JScrollPane(selectionArea));
		split.setDividerLocation(200);
		JTabbedPane tab = new JTabbedPane();
		tab.addTab("Interaction Event", split);
		tab.addTab("Layer Manager", new LayerManagerPane(network));
		
		JSplitPane leftSplit = new JSplitPane(
				JSplitPane.VERTICAL_SPLIT, new JScrollPane(tree), new TPropertySheetPane(sheet));
		
		JSplitPane rightSplit = new JSplitPane(
				JSplitPane.VERTICAL_SPLIT, network, tab);
		
		JSplitPane mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSplit, rightSplit);
		
		this.add(mainSplit, BorderLayout.CENTER);
		this.add(DemoUtil.createStatusLabel(network), BorderLayout.SOUTH);
		leftSplit.setDividerLocation(300);
		rightSplit.setDividerLocation(450);
		mainSplit.setDividerLocation(200);

		box.getLayerModel().addLayer(new Layer("textLayerID", "Text Layer"));
		box.addDataBoxListener(new DataBoxAdapter(){
		    public void elementAdded(DataBoxEvent e){
		    	Element element = e.getElement();
		    	if(element instanceof Text){
		    		element.setLayerID("textLayerID");
		    	}
		    }
		});
		
		ColorBackground background = new ColorBackground(Color.BLACK);
		background.setGradient(true);
		network.setBackground(background);
		
		network.setEnsureVisibleOnSelected(true);
		network.setEnableDeleteWithKeyboard(true);
		network.setEnableCopyPasteWithKeyboard(true);
		network.setToolbarByName("DrawingEditorToolbar");
		network.getToolbar().addSeparator();
		network.getToolbar().add(resizable);
		network.getToolbar().add(showGrid);
		resizable.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				network.getCanvas().repaint();
			}
		});
		showGrid.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				snapper.setShowGrid(showGrid.isSelected());
			}
		});
		network.setElementLabelEditableFilter(new EditableFilter(){
			public boolean isEditable(Element element) {
				return resizable.isSelected() && (element instanceof Text);
			}
		});
        network.setResizableFilter(new ResizableFilter(){
			public boolean isResizable(Element element) {
				return resizable.isSelected();
			}
		});
        network.clearMovableFilters();
        network.setPopupMenuGenerator(new PopupMenuGenerator(){
			public JPopupMenu generate(TView tview, MouseEvent mouseEvent) {
				JPopupMenu popMenu = new JPopupMenu();
				popMenu.add(new ClearDataBoxAction(network));
				return popMenu;
			}
        });
        
        // set tree
        tree.setEnsureVisibleOnSelected(true);

        // set propertySheet
        sheet.setEditable(true);

        try {
			box.parse("/demo/editor/drawing/drawing.xml");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		box.getUndoRedoManager().setLimit(200);
		network.setEnableUndoRedoWithKeyboard(true);
		
		final Map map = new HashMap();
		Field[] fields = InteractionEvent.class.getFields();
    	for(int i=0; i<fields.length; i++){
    		Field field = fields[i];
    		if(Modifier.isStatic(field.getModifiers())){
    			try {
					map.put(field.get(null), field.getName());
				} catch (Exception e1) {
					e1.printStackTrace();
				}
    		}
    	}
        network.addInteractionListener(new InteractionListener(){
			public void interactionPerformed(InteractionEvent event) {
				int type = event.getType();
				String name = (String)map.get(new Integer(type));
				if(name != null){
					intercationArea.append(name);
	                if(intercationArea.getLineCount() > 300){
	                	intercationArea.setText(null);
	                }
				}
			}
        });
        
        box.getSelectionModel().setSelectionChangedInterceptor(new SelectionChangedInterceptor(){
			public void beforeSelectionChanged(TDataBox box, int type, Collection elements) {
				selectionArea.append(">> Before:  " + getName(type) + " | Effected Count:" + elements.size());
			}
			public void afterSelectionChanged(TDataBox box, int type, Collection elements) {	
				selectionArea.append("<< After:   " + getName(type) + " | Effected Count:" + elements.size());
				if(selectionArea.getLineCount() > 300){
					selectionArea.setText(null);
                }
			}
			public String getName(int type){
				if(type == SelectionChangedInterceptor.APPEND_SELECTION_COLLECTION){
					return "APPEND_SELECTION_COLLECTION";
				}
				if(type == SelectionChangedInterceptor.APPEND_SELECTION_ELEMENT){
					return "APPEND_SELECTION_ELEMENT";
				}
				if(type == SelectionChangedInterceptor.CLEAR_SELECTION){
					return "CLEAR_SELECTION";
				}
				if(type == SelectionChangedInterceptor.REMOVE_SELECTION_COLLECTION){
					return "REMOVE_SELECTION_COLLECTION";
				}
				if(type == SelectionChangedInterceptor.REMOVE_SELECTION_ELEMENT){
					return "REMOVE_SELECTION_ELEMENT";
				}
				return "***";
			}
    	});
        
	}

}